/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bean.session;

import entity.DepartmentEntity;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author wei
 */
@Stateless
public class DepartmentEntityFacade extends AbstractFacade<DepartmentEntity> {

    @PersistenceContext(unitName = "MacTeam-warPU")
    private EntityManager em;

    protected EntityManager getEntityManager() {
        return em;
    }

    public DepartmentEntityFacade() {
        super(DepartmentEntity.class);
    }

    public boolean checkNameExist(String departmentName) {
        return findDepartmentByName(departmentName)!=null;
    }
    
    public DepartmentEntity findDepartmentByName(String name){
        DepartmentEntity department = null;
        Query query = (Query) em.createQuery("Select d from Department d WHERE d.name=:departmentName");
        query.setParameter("departmentName", name);

        try {
            department = (DepartmentEntity) query.getSingleResult();
        } catch (NoResultException e) {
        }

        return department;
    }

    public String[] getRolesInDepartment(String departmentName) {
        if (departmentName.equalsIgnoreCase("Sales")) {
            return new String[]{"salesManager", "salesEmployee"};
        }
        if (departmentName.equalsIgnoreCase("Marketing")) {
            return new String[]{"marketingManager", "marketingEmployee"};
        }
        if (departmentName.equalsIgnoreCase("Finance")) {
            return new String[]{"financeManager", "financeEmployee"};
        }
        return new String[]{"productionManager", "prodcutionEmployee"};
    }
}
